package com.beiding.delaytask.config;

import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.boot.autoconfigure.amqp.SimpleRabbitListenerContainerFactoryConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author 丁常磊
 * @date 2021/8/12 15:37
 */

@Configuration
public class DelayTaskRabbitConfig {

    @Bean("delayTaskCustomerFactory")
    public SimpleRabbitListenerContainerFactory containerFactory(SimpleRabbitListenerContainerFactoryConfigurer configurer,
                                                                 ConnectionFactory connectionFactory) {
        //核心线程数的两倍
        int size = Runtime.getRuntime().availableProcessors();
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        factory.setConcurrentConsumers(size);
        factory.setMaxConcurrentConsumers(size * 2);
        configurer.configure(factory, connectionFactory);
        return factory;
    }

}
